package com.clsfd.sms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.SQLException;

import com.clsfd.charging.ChargeEvent;
import com.clsfd.charging.ChargeStatus;
import com.clsfd.database.SystemParams;

public class SMSHandler {

	private String Username="";
	private String Password="";
	private String Server="localhost";
	private int Port=13000;
	public SMSHandler (SystemParams sysparams) throws SQLException{
		
		Username=sysparams.getSendSMSUserName();
		Password=sysparams.getSendSMSUserPassword();
		Server=sysparams.getSendSMSServer();
		Port=sysparams.getSendSMSPort();
	}
	
	public  SmsResponse  SendTextMessage(String toNumber, String fromNumber, String message) throws IOException, URISyntaxException{
		String response="99";
		URI sendsmsuri=  new URI("http", null, Server, Port, "/cgi-bin/sendsms", "username="+Username+"&password="+Password+"&from="+fromNumber+"&to="+toNumber+"&text="+message, null);
		URL sendsmsurl = sendsmsuri.toURL();
	    BufferedReader in = new BufferedReader(new InputStreamReader(sendsmsurl.openStream()));
		response=in.readLine();
		SmsResponse smsresp=new SmsResponse(); 
		smsresp.setResponse( response.split(": ")[0]);
		smsresp.setMessage( response.split(": ")[1]);
	    in.close();
		return smsresp;
	    		
	}
	
	public  SmsResponse  SendFlashMessage(String toNumber, String fromNumber, String message) throws IOException, URISyntaxException{
		String response="99";
		URI sendsmsuri=  new URI("http", null, Server, Port, "/cgi-bin/sendsms", "username="+Username+"&password="+Password+"&from="+fromNumber+"&mclass=0&to="+toNumber+"&text="+message, null);
		URL sendsmsurl = sendsmsuri.toURL();
	    BufferedReader in = new BufferedReader(new InputStreamReader(sendsmsurl.openStream()));
		response=in.readLine();
		SmsResponse smsresp=new SmsResponse(); 
		smsresp.setResponse( response.split(": ")[0]);
		smsresp.setMessage( response.split(": ")[1]);
	    in.close();
		return smsresp;
	    		
	}
}
